package handle

import (
	"context"
	"fmt"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
)

func NewFile() File {
	return File{}
}

type File struct {
}

func (r File) InitRoute(server *server.Hertz) {
	p := server.Group("/file")
	p.POST("/upload", r.Upload())
	p.POST("/uploads", r.FormFiles())
	p.POST("/uploadFormFiles", r.UploadFormFiles())
	p.POST("/sendFile", r.SendFile())
}

func (r File) Upload() app.HandlerFunc {
	return func(c context.Context, ctx *app.RequestContext) {
		// Set a lower memory limit for multipart forms (default is 32 MiB)
		// OR
		// app.Use(iris.LimitRequestBodySize(maxSize))
		// OR
		// OR iris.WithPOSTMaxMemory(maxSize)

		// single file
		fileHeader, err := ctx.FormFile("file")
		if err != nil {
			ctx.JSON(400, err)
			return
		}
		fmt.Println(fileHeader.Header, fileHeader.Size)

	}
}
func (r File) FormFiles() app.HandlerFunc {
	return func(c context.Context, ctx *app.RequestContext) {
		files, err := ctx.MultipartForm()
		if err != nil {
			ctx.SetStatusCode(400)
			return
		}
		for s, headers := range files.File {
			fmt.Println(s, headers)
			for i, header := range headers {
				fmt.Println(i, header.Header)
			}
		}
		for s, headers := range files.Value {
			fmt.Println(s, headers)
		}
	}
}
func (r File) UploadFormFiles() app.HandlerFunc {
	return func(c context.Context, ctx *app.RequestContext) {
		//ctx.SaveUploadedFile()
	}
}
func (r File) SendFile() app.HandlerFunc {
	return func(c context.Context, ctx *app.RequestContext) {
	}
}
